package com.baozhun.mall.common.widget.dialog

import android.content.Context
import android.view.View
import android.view.View.OnClickListener
import androidx.databinding.ViewDataBinding
import com.baozhun.mall.common.BR
import com.baozhun.mall.common.listener.ViewOnClickListener
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BottomPopupView
import me.hgj.jetpackmvvm.ext.bindingViewWithGeneric

/**
 * @Title：底部弹框base绑定基类，使用dataBinding时可继承此类,省去初始化ViewDataBinding的操作
 * @Description：
 * @Author：xuefeng.zhu
 * @Since：2022/11/28
 * @Version：1.0.0
 */
open class BaseBottomPopupView<T : ViewDataBinding>(context: Context) : BottomPopupView(context),
    ViewOnClickListener {

    lateinit var mBinding: T
    private var isDismissOnTouchOutside: Boolean = true
    private var autoFocusEditText: Boolean = false

    /**
     * 绑定的listener对象，默认为 **[BR.listener]**
     */
    protected open fun bindingListenerVariable(): Int {
        return BR.listener
    }

    /**
     * 绑定的listener对象，默认为 **[BR.viewModel]**
     */
    protected open fun bindingVmVariable(): Int {
        return BR.viewModel
    }

    override fun onCreate() {
        super.onCreate()
        mBinding = bindingViewWithGeneric(bottomPopupContainer.getChildAt(0))
        mBinding.setVariable(bindingListenerVariable(), this)
        mBinding.setVariable(bindingVmVariable(), this)
        mBinding.executePendingBindings()
    }

    override fun onClick(v: View?) {

    }

    fun dismissOnTouchOutside(isDismissOnTouchOutside: Boolean): BottomPopupView {
        this.isDismissOnTouchOutside = isDismissOnTouchOutside
        return this
    }

    open fun autoFocusEditText(autoFocusEditText: Boolean): BottomPopupView {
        this.autoFocusEditText = autoFocusEditText
        return this
    }

    override fun show(): BottomPopupView {
        XPopup.Builder(context)
            .dismissOnTouchOutside(isDismissOnTouchOutside)
            .autoFocusEditText(autoFocusEditText)
            .asCustom(this)
        super.show()
        return this
    }
}